package com.comp490.navigation;

import com.comp490.shooter.R;
import com.comp490.sound.SoundManager;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MainMenuOptionsFrag extends Fragment
{
    private CheckBox soundfx, music;
    private SharedPreferences soundoptions;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        soundoptions = getActivity().getSharedPreferences(
                SoundManager.SOUND_PREFS, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup,
            Bundle state)
    {
        return inflater
                .inflate(R.layout.mainmenu_optionsfrag, viewgroup, false);
    }

    @Override
    public void onActivityCreated(Bundle state)
    {
        super.onActivityCreated(state);

        music = (CheckBox) getActivity().findViewById(R.id.check_music_on);
        soundfx = (CheckBox) getActivity().findViewById(R.id.check_soundfx_on);

        Typeface photonica = Typeface.createFromAsset(this.getActivity()
                .getAssets(), "photonica_regular.ttf");
        Typeface arkitech = Typeface.createFromAsset(this.getActivity()
                .getAssets(), "arkitech_light.ttf");
        TextView title = (TextView) getActivity().findViewById(
                R.id.options_screentitle);

        title.setTypeface(photonica);
        music.setTypeface(arkitech);
        soundfx.setTypeface(arkitech);

        initializeCheckBoxes();

        music.setOnCheckedChangeListener(new MusicCheckListener());
        soundfx.setOnCheckedChangeListener(new SoundfxCheckListener());
    }

    @Override
    public void onPause()
    {
        super.onPause();
    }

    private void initializeCheckBoxes()
    {
        if(soundoptions.getBoolean(SoundManager.FX_ON, true))
            soundfx.setChecked(true);
        else
            soundfx.setChecked(false);

        if(soundoptions.getBoolean(SoundManager.MUSIC_ON, true))
            music.setChecked(true);
        else
            music.setChecked(false);
    }

    private class MusicCheckListener implements OnCheckedChangeListener
    {
        public void onCheckedChanged(CompoundButton view, boolean isChecked)
        {
            Editor editor = soundoptions.edit();

            if(isChecked)
            {
                SoundManager.unMuteMusic();
                SoundManager.playSoundEfx(SoundManager.BUTTON_CLICK);
                editor.putBoolean(SoundManager.MUSIC_ON, true);
            } else
            {
                SoundManager.muteMusic();
                SoundManager.playSoundEfx(SoundManager.BUTTON_CLICK);
                editor.putBoolean(SoundManager.MUSIC_ON, false);
            }

            editor.commit();
        }

    }

    private class SoundfxCheckListener implements OnCheckedChangeListener
    {
        public void onCheckedChanged(CompoundButton view, boolean isChecked)
        {
            Editor editor = soundoptions.edit();

            if(isChecked)
            {
                SoundManager.unMuteSoundFx();
                SoundManager.playSoundEfx(SoundManager.BUTTON_CLICK);
                editor.putBoolean(SoundManager.FX_ON, true);
            } else
            {
                SoundManager.muteSoundFx();
                editor.putBoolean(SoundManager.FX_ON, false);
            }

            editor.commit();
        }

    }

}
